package com.dodoke.service.impl;


import java.util.List;
import java.util.Map;

import com.dodoke.common.utils.Arith;
import com.dodoke.domain.WxOrder;
import com.dodoke.domain.vo.WxOrderVo;
import com.dodoke.mapper.WxOrderMapper;
import com.dodoke.service.IWxOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【订单】Service业务层处理
 *
 * @author dodoke
 * @date 2020-08-25
 */
@Service
public class WxOrderServiceImpl implements IWxOrderService
{
    @Autowired
    private WxOrderMapper wxOrderMapper;



    /**
     * 查询【订单】列表,给后台使用
     */
    @Override
    public List<WxOrderVo> selectWxOrderList4Back(WxOrder wxOrder) {
        return wxOrderMapper.selectWxOrderList4Back(wxOrder);
    }

    /***
     * 查询订单统计
     * @return
     */
    @Override
    public Map<Integer, Double> findOrderStat() {
        return wxOrderMapper.findOrderStat();
    }

    /***
     * 查询折线图所需的数据
     * @return
     * @param currentMonth
     */
    @Override
    public List<Map<String,Object>> findChartData(String currentMonth) {
        return wxOrderMapper.findChartData(currentMonth);
    }

    /***
     * 根据openId查询订单列表
     * @return
     * @param openId
     */
    @Override
    public List<WxOrder> selectWxOrderList(String openId) {
        return wxOrderMapper.selectWxOrderList(openId);
    }

    /**
     * 生成订单
     * 同时向统计表更新记录（新增一条订单量，新增加利润）
     *
     * @param wxOrder
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWxOrder(WxOrder wxOrder)
    {
        int affectNum = wxOrderMapper.insertWxOrder(wxOrder);
        double profit = Arith.sub(wxOrder.getGoodsCostPrice(),wxOrder.getGoodsWinningPrice());
        wxOrderMapper.updateOderStat(profit);
        return affectNum;
    }

}
